//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TimeHomogeneousForwardCorrelation.h"
using namespace Cephei::QL::Models::Marketmodels::Correlations;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (Cephei::QL::Math::IMatrix^ fwdCorrelation, Cephei::Core::IVector<Double>^ rateTimes) : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
    CMatrix^ _CfwdCorrelation;
    try
    {
#ifdef HANDLE
        _phTimeHomogeneousForwardCorrelation = NULL;
#endif
        _CfwdCorrelation = safe_cast<CMatrix^> (fwdCorrelation);
        _CfwdCorrelation->Lock();
        QuantLib::Matrix& _fwdCorrelation = static_cast<QuantLib::Matrix&> (_CfwdCorrelation->GetReference ()); 
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        _ppTimeHomogeneousForwardCorrelation = new boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation> (new QuantLib::TimeHomogeneousForwardCorrelation ( _fwdCorrelation,  _rateTimes ));
        SetPiecewiseConstantCorrelation (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfwdCorrelation != nullptr) _CfwdCorrelation->Unlock();
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation>& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phTimeHomogeneousForwardCorrelation = NULL;
#endif
	_ppTimeHomogeneousForwardCorrelation = &childNative;
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
}
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (QuantLib::TimeHomogeneousForwardCorrelation& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phTimeHomogeneousForwardCorrelation = NULL;
#endif
	_ppTimeHomogeneousForwardCorrelation = new boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation> (&childNative);
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
    _TimeHomogeneousForwardCorrelationOwner = owner;
    _PiecewiseConstantCorrelationOwner = owner;
}

Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (CTimeHomogeneousForwardCorrelation^ copy) : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phTimeHomogeneousForwardCorrelation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTimeHomogeneousForwardCorrelation = new boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation> (copy->GetShared());
        _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (PLATFORM::Type^ t) : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phTimeHomogeneousForwardCorrelation = NULL;
#endif
	if (!t->IsSubclassOf(CTimeHomogeneousForwardCorrelation::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (QuantLib::Handle<QuantLib::TimeHomogeneousForwardCorrelation>& childNative, Object^ owner)  : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
	_phTimeHomogeneousForwardCorrelation = &childNative;
	_ppTimeHomogeneousForwardCorrelation = &static_cast<boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
    _TimeHomogeneousForwardCorrelationOwner = owner;
}
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (QuantLib::Handle<QuantLib::TimeHomogeneousForwardCorrelation> childNative)  : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
	_phTimeHomogeneousForwardCorrelation = &childNative;
	_ppTimeHomogeneousForwardCorrelation = &static_cast<boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::CTimeHomogeneousForwardCorrelation (QuantLib::TimeHomogeneousForwardCorrelation childNative)  : CPiecewiseConstantCorrelation(CTimeHomogeneousForwardCorrelation::typeid)
{
#ifdef HANDLE
	_phTimeHomogeneousForwardCorrelation = NULL;
#endif
	_ppTimeHomogeneousForwardCorrelation = new boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation> (new QuantLib::TimeHomogeneousForwardCorrelation (childNative));
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppTimeHomogeneousForwardCorrelation));
}
#endif

Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::~CTimeHomogeneousForwardCorrelation ()
{
    if (_ppTimeHomogeneousForwardCorrelation != NULL)
    {
	    delete _ppTimeHomogeneousForwardCorrelation;
        _ppTimeHomogeneousForwardCorrelation = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::!CTimeHomogeneousForwardCorrelation ()
{
    if (_ppTimeHomogeneousForwardCorrelation != NULL)
    {
	    delete _ppTimeHomogeneousForwardCorrelation;
    }
}
QuantLib::TimeHomogeneousForwardCorrelation& Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::GetReference ()
{
    if (_ppTimeHomogeneousForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return **_ppTimeHomogeneousForwardCorrelation;
}
boost::shared_ptr<QuantLib::TimeHomogeneousForwardCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::GetShared ()
{
    if (_ppTimeHomogeneousForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return *_ppTimeHomogeneousForwardCorrelation;
}
QuantLib::TimeHomogeneousForwardCorrelation* Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::GetPointer ()
{
    if (_ppTimeHomogeneousForwardCorrelation == NULL) throw REFNEW NativeNullException ();
	return &**_ppTimeHomogeneousForwardCorrelation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TimeHomogeneousForwardCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::GetHandle ()
{
	if (_phTimeHomogeneousForwardCorrelation == NULL)
	{
		_phTimeHomogeneousForwardCorrelation = new Handle<QuantLib::TimeHomogeneousForwardCorrelation> (*_ppTimeHomogeneousForwardCorrelation);
	}
	return *_phTimeHomogeneousForwardCorrelation;
}
#endif
bool Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::HasNative () 
{
	return (_ppTimeHomogeneousForwardCorrelation != NULL);
}

Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::Correlations::get ()
{
    try
    {
    	std::vector<QuantLib::Matrix>& _rv = (std::vector<QuantLib::Matrix>&)(*_ppTimeHomogeneousForwardCorrelation)->correlations ( );   
        CoVector<Cephei::QL::Math::IMatrix^>^ _nrv = REFNEW CoVector<Cephei::QL::Math::IMatrix^>(REFNEW CMatrixVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::EvolvedMatrices (Cephei::QL::Math::IMatrix^ fwdCorrelation)
{
    CMatrix^ _CfwdCorrelation;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CfwdCorrelation = safe_cast<CMatrix^> (fwdCorrelation);
        _CfwdCorrelation->Lock();
        QuantLib::Matrix& _fwdCorrelation = static_cast<QuantLib::Matrix&> (_CfwdCorrelation->GetReference ()); 
    	std::vector<QuantLib::Matrix> _rv = (std::vector<QuantLib::Matrix>)(*_ppTimeHomogeneousForwardCorrelation)->evolvedMatrices ( _fwdCorrelation );   
        CoVector<Cephei::QL::Math::IMatrix^>^ _nrv = REFNEW CoVector<Cephei::QL::Math::IMatrix^>(REFNEW CMatrixVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfwdCorrelation != nullptr) _CfwdCorrelation->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTimeHomogeneousForwardCorrelation)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppTimeHomogeneousForwardCorrelation)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation::Times::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppTimeHomogeneousForwardCorrelation)->times ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Correlations::ITimeHomogeneousForwardCorrelation^ Cephei::QL::Models::Marketmodels::Correlations::CTimeHomogeneousForwardCorrelation_Factory::Create (Cephei::QL::Math::IMatrix^ fwdCorrelation, Cephei::Core::IVector<Double>^ rateTimes)
{
    return REFNEW CTimeHomogeneousForwardCorrelation ( fwdCorrelation,  rateTimes);
}
